/*
*pdflib测试代码:
*原作者的demo程序
*Created on: 2019-7-9
*Author: jun
*/
#include "spdf_ext.h"
#include "dataproc.h"
#include "math.h"

SPDF_Doc pdf;
SPDF_Page page;
static short cnt;

SPDF_Doc_Rec	pdf_doc;

void spdf_char_form_tab_create(void);
void spdf_api_tst(void);
void spdf_chart_demo(void);

int spdf_disk_out(uint8_t *buf, int len);

/*****************************************************************************
* @brief   author_demo_api
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void pdf_author_demo(void)
{
	SPDF_Obj * pobj;
	uint8_t * file_buff;
	
	pobj = malloc(sizeof(SPDF_Obj) * 128);
	file_buff = malloc(1024*4);
		
	spdf_init(&pdf_doc, pobj, 128);
	spdf_disk_buff_set(&pdf_doc, file_buff, 1024*4);
	spdf_disk_out_link(&pdf_doc, spdf_disk_out);
	pdf = spdf_create(&pdf_doc);
	
	cnt = 1;
	while(cnt --)
	{
		spdf_api_tst();
	}
	
	spdf_char_form_tab_create();
	
	spdf_chart_demo();
	
	spdf_save(pdf, "red.pdf");
	
	free(file_buff);
	free(pobj);
}

/*****************************************************************************
* @brief   spdf_char_form_tab_create 输出一页符号对照表.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void spdf_api_tst(void)
{
	spdf_custom_par_type cmd[4];
	
	page = spdf_add_page(pdf);
	
	//spdf_page_set_width(page, 297 * 3);
	//spdf_page_set_height(page, 210 * 3);
	
	
	spdf_page_set_font(page, "Courier-Bold", 24);
	
	spdf_page_set_stroke_rgb(page, 80, 60, 40);
	
	// 使用特效前保存状态
	spdf_page_push(page);
	
	// 开始文本
	spdf_page_begin_text(page);
	
	spdf_page_move_text_pos(page, 50, 600);
	
	spdf_page_set_text_mode(page, 1, 25);
	spdf_page_set_text_rgb(page, 100, 0, 0);
	spdf_page_show_text_gbk(page,"now R, mode 1 25");
	
	spdf_page_set_text_mode(page, 2, 25);
	spdf_page_move_text_pos(page, 0, -30);
	spdf_page_set_text_rgb(page, 0, 100, 0);
	spdf_page_show_text_gbk(page,"now G, mode 2 25");
	
	spdf_page_set_text_mode(page, 3, 25);
	spdf_page_move_text_pos(page, 0, -30);
	spdf_page_set_text_rgb(page, 0, 0, 100);
	spdf_page_show_text_gbk(page,"now B, mode 3 25");

	spdf_page_set_text_mode(page, 4, 25);
	spdf_page_move_text_pos(page, 0, -30);
	spdf_page_set_text_rgb(page, 0, 0, 100);
	spdf_page_show_text_gbk(page,"now B, mode 4 25");

	spdf_page_set_text_mode(page, 5, 25);
	spdf_page_move_text_pos(page, 0, -30);
	spdf_page_set_text_rgb(page, 0, 0, 100);
	spdf_page_show_text_gbk(page,"now B, mode 5 25");
	
	spdf_page_set_text_mode(page, 6, 25);
	spdf_page_move_text_pos(page, 0, -30);
	spdf_page_set_text_rgb(page, 0, 0, 100);
	spdf_page_show_text_gbk(page,"now B, mode 6 25");
	
	spdf_page_set_text_mode(page, 7, 25);
	spdf_page_move_text_pos(page, 0, -30);
	spdf_page_set_text_rgb(page, 0, 0, 100);
	spdf_page_show_text_gbk(page,"now B, mode 7 25");
	
	spdf_page_end_text(page);
	
	// 恢复状态
	spdf_page_pop(page);
	
	// 自定义命令测试
	spdf_custom_with_par(page, "",2,0x20);
	spdf_custom(page, " w\n");
	spdf_custom_cfg(&cmd[0]," ", 100, 0x22, 0);
	spdf_custom_cfg(&cmd[1]," ", 10, 0x22, 0);
	spdf_custom_cfg(&cmd[2]," RG\n", 50, 0x22, SPDF_CMD_End);
	spdf_custom_exe(page, cmd, 3);
	
	spdf_custom_cfg(&cmd[0]," ", 500, 0x40, 0);
	spdf_custom_cfg(&cmd[1]," m\n", 500, 0x40, SPDF_CMD_End);
	spdf_custom_exe(page, cmd, 3);
	
	spdf_custom_cfg(&cmd[0]," ", 400, 0x40, 0);
	spdf_custom_cfg(&cmd[1]," l\nS\n", 400, 0x40, SPDF_CMD_End);
	spdf_custom_exe(page, cmd, 3);
	
	
	spdf_custom(page, "\nBT\n/F2 24 Tf\n0 Tr\n(abc) Tj\nET\n");

	spdf_page_save(page);
}

/*****************************************************************************
* @brief   spdf_char_form_tab_create 输出一页符号对照表.
* @param   none
* @return  none
* @ Pass/ Fail criteria: none
*****************************************************************************/
void spdf_char_form_tab_create(void)
{
	int cnt, gp;
	uint8_t buff[10];
	spdf_color_type fcolor, bcolor;
	
	spdf_form_type form = { 0 };
	
	page = spdf_add_page(pdf);
	spdf_form_page_mount(&form, page);

	spdf_page_push(page);
	spdf_page_dash_on(page, 35, 25, 0x21);
	
	spdf_page_endpoint_mode(page, 1);
	
	form.frame.ext_dash_len1 = 30;
	form.frame.ext_dash_len0 = 20;
	form.frame.inn_dash_len1 = 10;
	form.frame.inn_dash_len0 = 30;
	
	for (gp = 1; gp <= 3; gp++)
	{
		spdf_form_col_set(&form, 4, 0, 40);     // 列设置
		spdf_form_row_set(&form, 32, 24);        //行设置
		spdf_form_frame_ext_set(&form, 100, 10, 10, 50);
		spdf_form_frame_inn_set(&form, 30, 40, 40, 80);
		spdf_form_text_offset(&form, 3, 5);
		spdf_form_text_font(&form, "SimSun", 18);
		spdf_form_text_alain(&form, SPDF_FORM_ALAIN_MID);
		spdf_form_org(&form, 50 + 170 * (gp - 1), 40);

		spdf_form_draw(&form);
		spdf_form_text_start(&form);
		spdf_form_text_font(&form, "SimSun", 20);

		spdf_page_set_text_rgb(page, 100, 50, 0);

		fcolor.r = 0; fcolor.g = 30; fcolor.b = 60;
		bcolor.r = 100; bcolor.g = 70; bcolor.b = 40;
		for (cnt = 0x20 * gp; cnt < 0x20 * gp + 32; cnt++)
		{
			hex2_to_ascii(cnt, buff);
			buff[2] = 0;
			spdf_form_text_auto_ext(&form, (void*)buff,&bcolor,&fcolor);
			spdf_form_text_next_row(&form);
		}

		for (cnt = 0x20 * gp; cnt < 0x20 * gp + 32; cnt++)
		{
			buff[0] = cnt;
			buff[1] = 0;
			spdf_form_text_auto(&form, (void*)buff);
			spdf_form_text_next_row(&form);
		}

		spdf_form_text_font(&form, "Symbol", 20);
		for (cnt = 0x20 * gp; cnt < 0x20 * gp + 32; cnt++)
		{
			buff[0] = cnt;
			buff[1] = 0;
			spdf_form_text_auto(&form, (void*)buff);
			spdf_form_text_next_row(&form);
		}
		spdf_form_text_font(&form, "ZapfDingbats", 20);
		for (cnt = 0x20 * gp; cnt < 0x20 * gp + 32; cnt++)
		{
			buff[0] = cnt;
			buff[1] = 0;
			spdf_form_text_auto_ext(&form, (void*)buff,&bcolor,&fcolor);
			spdf_form_text_next_row(&form);
		}

		// 等宽坐标可以使用负标
		spdf_page_set_text_rgb(page, 0, 30, 60);
		spdf_form_text_font(&form, "SimSun", 16);
		
		fcolor.r = 0; fcolor.g = 30; fcolor.b = 60;
		bcolor.r = 100; bcolor.g = 70; bcolor.b = 40;
		spdf_form_text_ext(&form, -1, 0, "HEX", &bcolor, &fcolor);
		spdf_form_text(&form, -1, 1, "ASC");
		spdf_form_text(&form, -1, 2, "Sym");
		spdf_form_text_ext(&form, -1, 3, "Zap", &bcolor, &fcolor);
		
		//spdf_form_text_sp(&form, 0, 0, 5, 5, "num");
		
		spdf_form_text_end(&form);
	}

	//spdf_form_line_sp(&form, 0, 0, 0, 24,  40, 0);
	
	spdf_page_pop(page);
	
	spdf_page_save(page);
}

////////////////////////////////////////////////////////////////////////////////
spdf_chart_ytxt_type y_txt[2] =
{ 0 };

// 横线列表
const spdf_chart_hl_type crt_hl[4] =
{
	{ { 50, 0, 50, 0 }, 350, 50, 15 },
};

// 垂线列表
const spdf_chart_vl_type crt_xfg[4] =
{
	{  32,  30, 5, 30, 15 },
	{  256, 30, 5, 30, 15 },
	{  512, 30, 5, 30, 15 },
};

spdf_chart_line_type line_cfg[2] = { 0 };

ii16 line_val_list[1024]={0};

void spdf_chart_demo(void)
{
	ii16 cnt;
	
	spdf_chart_type chart = { 0 };
	
	page = spdf_add_page(pdf);
	spdf_page_set_width(page, 297 * 3);
	spdf_page_set_height(page, 210 * 3);
	
	// 曲线页面挂载
	spdf_chart_mount(&chart, page);

	// 区域配置
	chart.x0 = 450;
	chart.y0 = 450;
	chart.height = 5000;
	chart.width = 8000;
	chart.dir = 0;		//曲线方向，仅0有坐标文本
	
	// 轴及网格配置
	spdf_chart_axis(&chart, 100, 0, 25, 75);	//轴-线宽，RGB
	spdf_chart_grid_m(&chart, 30, 85, 85, 90);	//主网格-线宽，RGB
	spdf_chart_grid_s(&chart, 15, 95, 95, 100);	//子网格-线宽，RGB
	chart.frame.mode = 1;				// 0-仅轴，1-网格，2刻度				
	chart.frame.scale_m_size = 25;		// 刻度模式，主刻度长度
	chart.frame.scale_s_size = 15;		// 刻度模式，子刻度长度
	chart.frame.grid_x_m_wide = 500;	// x向，主网格间距
	chart.frame.grid_y_m_wide = 500;	// y向，主网格间距
	chart.frame.grid_x_s_num = 5;		// x向，子网格数量
	chart.frame.grid_y_s_num = 5;		// y向，子网格数量
	chart.frame.arrows_offset = 70;		// 箭头位置，若无箭头时非零补充右上边框
	chart.frame.arrows_size = 50;		// 箭头尺寸，设为0无箭头
	
	// 空间横线
	chart.frame.hl = crt_hl;			// 线配置
	chart.frame.hl_amount = arr_len(crt_hl);	// 数量
	
	// 生成线数据
	for(cnt=0; cnt < sizeof(line_val_list)/sizeof(line_val_list[0]); cnt++)
	{
		float site;
		site = (float)10*cnt * 3.14 /(sizeof(line_val_list)/sizeof(line_val_list[0]));
		line_val_list[cnt] = (ii16)(sin(site) * 1000);
	}
	
		// 垂线数量

	// 线配置
	

	//x轴文本配置
	chart.txt.x_mode = 2;		//模式-//0-数值 //1-rtc-tm // 2-rtc/td //3-rtc/dt
	chart.txt.x_val_ipn = 0x22;	//值整数小数
	chart.txt.x_val_start = 0;	//值起始值
	chart.txt.x_val_inc = 30;	//值/时间增量
	chart.txt.x_r = 100;		//文本颜色
	chart.txt.x_g = 0;
	chart.txt.x_b = 100;
	chart.txt.x_rtc.time.year = 20;//起始时间
	chart.txt.x_rtc.time.mon = 8;
	chart.txt.x_rtc.time.day = 15;
	chart.txt.x_rtc.time.hour = 23;
	chart.txt.x_rtc.time.min = 30;

	// Y轴文本配置 - 多文本
	y_txt[0].r = 100;
	y_txt[0].ipn = 0x32;
	y_txt[0].unit = "K";
	y_txt[0].val_start = -2000;
	y_txt[0].val_inc = 1000;
	y_txt[0].val_attr = 0x10;	// 值转换属性，参考i32_to_string
	
	y_txt[1].b = 100;			// 颜色
	y_txt[1].ipn = 0x40;		// 值格式
	y_txt[1].unit = "RH";		// 后缀
	y_txt[1].val_start = 0;		// 开始值
	y_txt[1].val_inc = 500;		// 值增量
	
	chart.txt.ytxt = y_txt;		// Y文本配置列表
	chart.txt.y_txt_num = 2; 	// Y轴文本数量
	
	// 轴文本统配
	chart.txt.font_size = 8;		// 字号
	chart.txt.font_name = 0;		// 字体，为0时，默认SunSim
	chart.txt.enable = 1;			// 开关
	chart.txt.ytxt_dx = 20;	// y文本位置偏移
	chart.txt.ytxt_dy = -5;	// x文本位置偏移

	// 曲线配置 - 多线
	chart.line_list = line_cfg;
	chart.line_num = 2;
		
	spdf_chart_show(&chart);
	
	spdf_page_save(page);
	
}

